import io
import re
import sys
import json
import jieba
import jieba.analyse as anls


import jionlp
from py2neo import Relationship
from py2neo import Graph, Node


sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
#
# 接收 Java 传递过来的参数
received_str = sys.argv[1]
print(received_str)  # 打印接收到的参数

# 解析 JSON 字符串为 Python 字典
data = json.loads(received_str)

## 去除大括号和空格
#cleaned_str = received_str.strip('{}').replace(' ', '')
#
## 将字符串按逗号分割成键值对列表
#key_value_pairs = cleaned_str.split(',')
#
## 创建一个字典，用于存储键值对
#data = {}
#for pair in key_value_pairs:
#    key, value = pair.split(':')
#    key = key.strip()
#    value = value.strip()
#    data[key] = value
## 输出提取出的键值对
#print(data.get("address"))
#for key, value in data.items():
#    print(f'{key}: {value}')

graph = Graph("http://47.109.43.77/:7474", auth=("neo4j", "Xiyou666"), name="neo4j")
# 删除知识图谱中所有节点
# graph.delete_all()

#创建job节点
job = Node('job', id=data.get("id"), company=data.get("company"), name=data.get("name"), salary=data.get("salary"), education=data.get("education"),
           description=data.get("description"), hiring_manager=data.get("hiring_manager"), last_active=data.get("last_active"), address=data.get("address"),
           link=data.get("link"))
graph.create(job)


location = jionlp.parse_location(data.get("address"))
# 通过name属性匹配或创建城市节点
query1 = """
MERGE (city:City {province: $province, name: $cityName})
RETURN city
"""
# 执行查询并传入参数
result1 = graph.run(query1, province=location.get("province"), cityName=location.get("city")).data()
# 通过name属性匹配或创建公司节点
query2 = """
       MERGE (company:Company {name: $companyName})
       RETURN company
       """
# 执行查询并传入参数
result2 = graph.run(query2, companyName=data.get("company")).data()

# 创建公司与城市节点之间的关系
query3 = """
MATCH (city:City {province: $province, name: $cityName}), (company:Company {name: $companyName})
MERGE (company)-[r:LOCATED_IN]->(city)
RETURN r
"""
# 执行查询并传入参数
result3 = graph.run(query3, province=location.get("province"), cityName=location.get("city"), companyName=data.get("company")).data()

# 创建公司与职位节点之间的关系
query4 = """
MATCH (company:Company {name: $name})
MATCH (job:job {id: $id})
MERGE (job)-[:BELONG_TO]->(company)
   """
result4 = graph.run(query4, name=data.get("company"), id=data.get("id"))

# 建立职位与城市之间的关系
query5 = """
  MATCH (job:job {id: $id}),
     (city:City {province: $province, name: $cityName})
MERGE (job)-[r:LOCATED_IN]->(city)
RETURN r
  """
# 执行查询并传入参数
result5 = graph.run(query5, id=data.get("id"), province=location.get("province"), cityName=location.get("city"),
                    companyName=data.get("company")).data()
result1 = []
data0 = {}
def process_job_data(data):
    if '前端' in data:
        data0['job'] = data0.get('job', '') + '/前端'

    if '后端' in data:
        data0['job'] = data0.get('job', '') + '/后端'

    if 'Java' in data or 'java' in data or 'JAVA' in data:
        data0['job'] = data0.get('job', '') + "/Java"

    if 'Python' in data or 'python' in data:
        data0['job'] = data0.get('job', '') + "/Python"

    if 'Html' in data or 'HTML' in data or 'H5' in data or 'h5' in data:
        data0['job'] = data0.get('job', '') + "/HTML"

    if 'JavaScript' in data or 'JS' in data:
        data0['job'] = data0.get('job', '') + "/JavaScript"

    if 'Vue' in data or 'vue' in data:
        data0['job'] = data0.get('job', '') + "/Vue"

    if '小程序' in data:
        data0['job'] = data0.get('job', '') + "/小程序"

    if 'uniapp' in data or 'Uniapp' in data:
        data0['job'] = data0.get('job', '') + "/uniapp"

    if 'C++' in data or 'c++' in data:
        data0['job'] = data0.get('job', '') + "/C++"

    if 'C#' in data or 'c#' in data:
        data0['job'] = data0.get('job', '') + "/C#"

    if 'Linux' in data or 'linux' in data:
        data0['job'] = data0.get('job', '') + "/Linux"

    if 'Php' in data or 'php' in data or 'PhP' in data or 'PHP' in data:
        data0['job'] = data0.get('job', '') + "/php"

    if 'Go' in data or 'go' in data and 'golang' not in data and 'Golang' not in data:
        data0['job'] = data0.get('job', '') + "/Go"

    if 'Golang' in data or 'golang' in data:
        data0['job'] = data0.get('job', '') + "/Golang"

    if 'unity' in data or 'Unity' in data:
        data0['job'] = data0.get('job', '') + "/Unity"

    if 'android' in data or 'Android' in data:
        data0['job'] = data0.get('job', '') + "/Android"

    if 'plc' in data or 'PLC' in data:
        data0['job'] = data0.get('job', '') + "/plc"

    if 'bim' in data or 'BIM' in data:
        data0['job'] = data0.get('job', '') + "/bim"

    if 'Xpon' in data:
        data0['job'] = data0.get('job', '') + "/Xpon"

    if 'fae' in data or 'FAE' in data:
        data0['job'] = data0.get('job', '') + "/fae"

    if 'layout' in data or 'Layout' in data:
        data0['job'] = data0.get('job', '') + "/layout"

    if '软件' in data:
        data0['job'] = data0.get('job', '') + "/软件类"

    if '硬件' in data:
        data0['job'] = data0.get('job', '') + "/硬件类"

    if '研发' in data:
        data0['job'] = data0.get('job', '') + "/研发类"

    if '开发' in data:
        data0['job'] = data0.get('job', '') + "/开发类"

    if '电子' in data:
        data0['job'] = data0.get('job', '') + "/电子类"

    if '通信' in data:
        data0['job'] = data0.get('job', '') + "/通信类"

    if '计算机' in data:
        data0['job'] = data0.get('job', '') + "/计算机类"

    if '游戏' in data:
        data0['job'] = data0.get('job', '') + "/游戏开发类"

    if '大数据' in data:
        data0['job'] = data0.get('job', '') + "/大数据类"

    if '信息' in data:
        data0['job'] = data0.get('job', '') + "/信息类"

    if '电气' in data:
        data0['job'] = data0.get('job', '') + "/电气类"

    if '弱电' in data:
        data0['job'] = data0.get('job', '') + "/弱电类"

    if '测试' in data:
        data0['job'] = data0.get('job', '') + "/测试类"

    if '嵌入' in data:
        data0['job'] = data0.get('job', '') + "/嵌入式开发"

    if '运维' in data:
        data0['job'] = data0.get('job', '') + "/运维"

    if '网络' in data:
        data0['job'] = data0.get('job', '') + "/网络类"

    if '算法' in data:
        data0['job'] = data0.get('job', '') + "/算法类"

    if '专利' in data:
        data0['job'] = data0.get('job', '') + "/专利类"

    if '网页' in data or '网站' in data:
        data0['job'] = data0.get('job', '') + "/网页开发类"

    if '助理' in data:
        data0['job'] = data0.get('job', '') + "/助理类"

    if '验证' in data:
        data0['job'] = data0.get('job', '') + "/验证类"

    if '实施' in data:
        data0['job'] = data0.get('job', '') + "/实施类"

    if '调试' in data:
        data0['job'] = data0.get('job', '') + "/调试类"

    if '工艺' in data:
        data0['job'] = data0.get('job', '') + "/工艺类"

    if '安装' in data:
        data0['job'] = data0.get('job', '') + "/安装类"

    if '服务' in data:
        data0['job'] = data0.get('job', '') + "/服务类"

    if '检测' in data:
        data0['job'] = data0.get('job', '') + "/检测类"

    if '设备' in data:
        data0['job'] = data0.get('job', '') + "/设备类"

    if '工控' in data:
        data0['job'] = data0.get('job', '') + "/工控类"

    if '机械' in data:
        data0['job'] = data0.get('job', '') + "/机械类"

    if '测绘' in data:
        data0['job'] = data0.get('job', '') + "/测绘类"

    if '芯片' in data:
        data0['job'] = data0.get('job', '') + "/芯片类"

    if '光学' in data:
        data0['job'] = data0.get('job', '') + "/光学类"

    if '物料' in data:
        data0['job'] = data0.get('job', '') + "/物料类"

    if '调试' in data:
        data0['job'] = data0.get('job', '') + "/调试类"

    if '结构' in data:
        data0['job'] = data0.get('job', '') + "/结构类"

    if '物流' in data:
        data0['job'] = data0.get('job', '') + "/物流类"

    if '智能' in data:
        data0['job'] = data0.get('job', '') + "/智能类"

    if '水电' in data:
        data0['job'] = data0.get('job', '') + "/水电类"

    if '上位机' in data:
        data0['job'] = data0.get('job', '') + "/上位机类"

    if '节能' in data:
        data0['job'] = data0.get('job', '') + "/节能类"

    if '土建' in data:
        data0['job'] = data0.get('job', '') + "/土建类"

    if '土木' in data:
        data0['job'] = data0.get('job', '') + "/土木类"

    if 'IT' in data or '程序员' in data:
        data0['job'] = data0.get('job', '') + "/IT类"

    if '销售' in data or '售前' in data or '售后' in data or '售中' in data:
        data0['job'] = data0.get('job', '') + "/销售类"

    if '建筑' in data or '路' in data or '桥' in data:
        data0['job'] = data0.get('job', '') + "/建筑类"

    if '系统' in data:
        data0['job'] = data0.get('job', '') + "/系统类"

    if '造价' in data:
        data0['job'] = data0.get('job', '') + "/造价类"

    if '监理' in data:
        data0['job'] = data0.get('job', '') + "/监理类"

    if '维护' in data:
        data0['job'] = data0.get('job', '') + "/维护类"

    if '技术' in data:
        data0['job'] = data0.get('job', '') + "/技术类"

    if not data0.get('job'):
    	   data0['job'] = "/其他"
    # 工作分类
    if '实习' in data or '实习' in data:
        data0['category'] = data0.get('category', '') + "/实习"
    if '应届' in data or '应届' in data:
        data0['category'] = data0.get('category', '') + "/应届"
    if not data0.get('category'):
        data0['category'] = "/其他"

    result1.append(data0)
    return result1

jobData = process_job_data(data.get("name"))
# 获取类别名称并添加到集合中
category_parts = jobData[0].get("job").split("/")[1:]


# 获取需求名称并添加到集合中
demand_parts = jobData[0].get("category").split("/")[1:]

# 创建类别节点
for category in category_parts:
    query1 = """
       MERGE (category:skills {name: $categoryName})
       RETURN category
       """
    # 执行查询并传入参数
    result = graph.run(query1, categoryName=category).data()

# 创建需求节点
for demand in demand_parts:
    query2 = """
       MERGE (demand:Demand {name: $demandName})
       RETURN demand
       """
    # 执行查询并传入参数
    result = graph.run(query2, demandName=demand).data()

categories1 = set()
category_parts1 = jobData[0].get("job").split("/")[1:]
# 创建职位与类别节点的关系
for category in category_parts1:
    query_category = """
       MATCH (job:job {id: $id})
       MATCH (skills:skills {name: $categoryName})
       MERGE (job)-[:REQUIRES]->(skills)
       """
    # 执行查询并传入参数
    result_category = graph.run(query_category, id=data.get("id"), categoryName=category).data()
demands1 = set()
demand_parts1 = jobData[0].get("category").split("/")[1:]
# 创建职位与需求节点的关系
for demand in demand_parts1:
    query_demand = """
          MATCH (job:job {id: $id})
          MATCH (demand:Demand {name: $demandName})
          MERGE (job)-[:HAS_DEMAND]->(demand)
          """
    # 执行查询并传入参数
    result_demand = graph.run(query_demand, id=data.get("id"), demandName=demand).data()
#
def extract_minimum_salary(salary_str):
    # 使用正则表达式查找薪资数字
    match = re.search(r'(\d+)-(\d+)K', salary_str)
    if match:
        # 返回最低薪资
        return int(match.group(1)) * 1000  # 将 'K' 转换为千
    else:
        return None


salaryData = extract_minimum_salary(data.get("salary"))
# salaryData[i][2].strip()  # 删除s开头、结尾的rm
# salaryData[i][2].split(".")  # 删除s开头的rm
# 获取工资数值
salary = float(salaryData)
salary2 = salaryData

# 创建工资节点
query = """
MERGE (salary:Salary {name: $salaryName ,salary: $salary})
RETURN salary
"""

#执行查询并传入参数
result = graph.run(query, salaryName=salary2, salary=salary).data()
# 建立关系
relation_query = """
    MATCH (job:job {id: $id})
    MATCH (salary:Salary {name: $salaryName})
    MERGE (job)-[r:HAS_SALARY]->(salary)
    RETURN r
    """

# 执行查询并传入参数
relation = graph.run(relation_query, id=data.get("id"), salaryName=salary).data()
print("Ok")
